﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using web.core.ViewModels;
using web.core.Models;
using web.component;

namespace web.core.Controllers
{
    public class admClientContactController : baseController
    {
        // danh sách các trạng thái đọc tin
        private List<ContactClientIsReadItem> GetListIsRead() {
            var lst = new List<ContactClientIsReadItem>()
            {
                 new ContactClientIsReadItem{ key =false,value="Chưa đọc"},
                 new ContactClientIsReadItem{ key =true,value="Đã đọc"},
            };
            return lst;
        }
        //------------------------------------
        //contact_list
        //------------------------------------
        #region contact_list PAGE
        public ActionResult contact_list()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult get_list(int? pageindex)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);

            return get_list(pageindex, admin_const.PAGE_LIST_SIZE);
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult get_list(int? pageindex, int? pagesize)
        {
            var lstdelete = Request.Form["is_delete"];
            if (!string.IsNullOrEmpty(lstdelete))
            {
                var arr = lstdelete.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var iterator in arr)
                {
                    if (iterator != "false")
                        clientContactModels.deleteItem(Convert.ToInt64(iterator));
                }
            } 
            var lstRead = GetListIsRead();
            var page_view = new ViewModels.ContactClientPageListViewModels()
            { 
                //list_group = accountModels.getListUserGroup(),
                list_pagesize = GetSizePagingPublic(pagesize ?? admin_const.PAGE_LIST_SIZE),
                pagesize = pagesize ?? admin_const.PAGE_LIST_SIZE, 
            };
            TryUpdateModel(page_view);
            bool? isread = null;
            if (page_view.is_read != null)
            {
                isread = page_view.is_read;
            }
            //var isread = (is_read ?? 0);
            //var lst_lang = languageModels.get_list_language();
            //page_view.select_list_language = new SelectList(lst_lang, "language_id", "language_desc");
            page_view.select_list_is_read = new SelectList(lstRead, "key", "value");

            page_view.page_list_contact = clientContactModels.client_contact_plist(pageindex ?? 1, pagesize ?? admin_const.PAGE_LIST_SIZE, isread);
            return PartialView("../Shared/client_contact/list_contact", page_view);
        } 
        #endregion

        //------------------------------------
        //contact_view
        //------------------------------------
        #region contact_view PAGE
        public ActionResult contact_view()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ctr_contat_view(int? id)
        {
            int itemid = id ?? 0;
            //if (itemid == 0)
            //    ViewBag.isAdd = true;
            //else
            //    ViewBag.isAdd = false;

            ViewModels.ContactClientViewModels item_view;
            //var item_view = new ViewModels.LanguageViewModels();
            var it = clientContactModels.getByID(itemid);
            if (it != null)
            {
                //string sUrl = siteSettingSingleModels.GetConfig(global_const.WEBSITE_URL);
                item_view = new ContactClientViewModels()
                {
                    contact_id = it.contact_id,
                    contact_name = it.contact_name,
                    contact_email = it.contact_email,
                    contact_phone = it.contact_phone,
                    contact_address = it.contact_address,
                    title = it.title,
                    content_full = it.content_full,
                    sent_date = Udate_time.GetTimeByCountryDefault(it.sent_date ?? DateTime.MinValue),
                    is_read = it.is_read ?? false,
                };
                clientContactModels.updateStatus(it.contact_id, true);// cap nhat trang thai doc.
            }
            else
                item_view = new ContactClientViewModels()
                {
                    
                };

            return PartialView("../Shared/client_contact/view_contact", item_view);
        }

        //[ChildActionOnly]
        //[ValidateInput(false)]
        //[HttpPost]
        //public ActionResult ctr_contat_view(FormCollection collection)
        //{
        //    var item_view = new ViewModels.PageStaticViewModels();
        //    TryUpdateModel(item_view);

        //    if (string.IsNullOrEmpty(item_view.page_name) || string.IsNullOrEmpty(item_view.link))
        //    {
        //        ViewBag.result_message = "Tên và Link của trang không để trống.";
        //        ViewBag.result = false;
        //        return PartialView("../Shared/page_static/edit_page", item_view);
        //    }
        //    bool isdel = item_view.page_id != 0 ? true : false;
        //    var item = new data.entities.page_static()
        //    {
        //        page_id = item_view.page_id,
        //        page_name = item_view.page_name,
        //        link = item_view.link,
        //        //image_path = item_view.image_path,
        //        image_path = SaveImage(isdel, item_view.image_path, item_view.fileUpload, admin_const.E_IMG_TYPE.PAGE_STATIC, admin_const.E_IMG_THUMB.NO_THUMB),
        //        summary = item_view.summary,
        //        page_content = item_view.page_content,
        //        date_modified = Udate_time.GetCurrentDateTime(),
        //        show = item_view.show,
        //        language_id = item_view.language_id,
        //        keyword = item_view.keyword,
        //        description = item_view.description,
        //        title = item_view.title,
        //        order_display = item_view.order_display,
        //        on_menu = item_view.on_menu
        //    };

        //    int ret_id = pageStaticModels.adm_page_static_update(item);

        //    if (ret_id > 0)
        //    {
        //        ViewBag.result_message = admin_const.UPDATE_OK;
        //        ViewBag.redirect = "/vi/admClientContact/page_list";
        //        return PartialView("../Shared/control_redirect");
        //    }
        //    else
        //    {
        //        ViewBag.result_message = admin_const.UPDATE_FAIL;
        //        ViewBag.result = false;
        //        return PartialView("../Shared/page_static/edit_page", item);
        //    }
        //} 
        #endregion
    }
}